﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using com.codeplex.eatoutadvocate.Engine;

namespace com.codeplex.eatoutadvocate.Web
{
    public partial class Recommend : System.Web.UI.Page
    {
        string zipCode;

        /// <summary>
        /// Page initialization
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Init(object sender, EventArgs e)
        {
            //Set dropdown items for Mood from Mood Enum
            DropDownMood.DataSource = Enum.GetNames(typeof(Engine.Mood));
            DropDownMood.DataBind();

            //Set dropdown items with values 1-3
            int[] moodLevel = new int[] { 1, 2, 3 };
            DropDownMoodLvl.DataSource = moodLevel;
            DropDownMoodLvl.DataBind();
        }

        /// <summary>
        /// Page Load
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e) 
        {
            zipCode = "";
            if (HttpContext.Current.Request.Cookies["ZipCode"] != null &&
                HttpContext.Current.Request.Cookies["ZipCode"].Value != "")
            {
                zipCode = HttpContext.Current.Request.Cookies["ZipCode"].Value;
                TxtBoxZipCode.Text = zipCode;
            }
        }
        
        /// <summary>
        /// Performs a set of actions if dropdown items selected in Mood change 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void DropDownMood_SelectedIndexChanged(object sender, EventArgs e)
        {
            //Check if selected value is Neutral or Drunk
            //If true, hide mood level dropbox as those do not have levels
            if (DropDownMood.SelectedValue == "Neutral" || DropDownMood.SelectedValue == "Drunk")
            {
                DropDownMoodLvl.Visible = false;
                LblMoodLvl.Text = "";
            }
            else
            {
                DropDownMoodLvl.Visible = true;
                LblMoodLvl.Text = "Mood Level: ";
            }
        } 

        /// <summary>
        /// On recommend button click
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void BtnRecommend_Click(object sender, EventArgs e)
        {

            if (! SanitaryInput())
            {
                return;
            }
            List<string> restaurants;                   //List to store recommended restaurants
            //Get new BL recommendation engine object
            BL.Recommender recommender = BL.Recommender.getNewRecommender();
            recommender.WeatherAppKey = "19dc467061c361d4";                     //Weather API KEY
            DateTime time = DateTime.Now;                                       //Time
            //Value for Mood
            Engine.Mood mood = (Engine.Mood)Enum.Parse(typeof(Engine.Mood), DropDownMood.SelectedValue, true);
            //Location
            int moodLevel;                          //Mood level on if mood level applicable
            if (DropDownMoodLvl.Visible == true)
            {
                moodLevel = Int16.Parse(DropDownMoodLvl.SelectedValue);
            }
            else
            {
                moodLevel = 0;
            }
            
            int userID = 14;                        //User ID -- TODO link up to FB Access token

            zipCode = TxtBoxZipCode.Text;
            AddCookie("ZipCode", zipCode);
            //Build user
            recommender.BuildUser(time, mood, moodLevel, zipCode);
            //Get recommendation
            restaurants = recommender.GetRestaurant(userID);
            //Display recommendations
            Recommendation.Text = "";
            Recommendation.Text += "<h1><br />";
            foreach (string restaurant in restaurants)
            {
                Recommendation.Text += restaurant;
                Recommendation.Text += "<br />";
            }
            Recommendation.Text += "</h1>";

        }


        private bool SanitaryInput()
        {
            Regex re = new Regex("^[a-zA-Z]*$");
            if (!re.IsMatch(DropDownMood.Text))
            {
                HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=\"JavaScript\">alert(\"Invalid Characters in Mood\")</SCRIPT>");
                return false;
            }

            re = new Regex("^[0-9]{5}$");
            if (!re.IsMatch(TxtBoxZipCode.Text))
            {
                HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=\"JavaScript\">alert(\"Invalid Zip Code\")</SCRIPT>");
                return false;
            }

            re = new Regex("^[1-3]$");
            if (!re.IsMatch(DropDownMoodLvl.Text))
            {
                HttpContext.Current.Response.Write("<SCRIPT LANGUAGE=\"JavaScript\">alert(\"Invalid Characters in Mood Level\")</SCRIPT>");
                return false;
            }

            return true;
        }

        private void AddCookie(string key, string value)
        {
            HttpCookie cookie = new HttpCookie(key, value);
            cookie.Expires.AddDays(7.0);
            Response.Cookies.Add(cookie);
        }
    }
}